import com.lijinjiang.beautyeye.BeautyEyeLNFHelper;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;

/**
 * @ClassName Demo01
 * @Description TODO
 * @Author Li
 * @Date 2022/9/16 16:26
 * @ModifyDate 2022/9/16 16:26
 * @Version 1.0
 */
public class Demo01 {
    public static void main(String[] args) {
        try {
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
            BeautyEyeLNFHelper.launchBeautyEyeLNF();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JFrame frame = new JFrame("Demo01");
        frame.setSize(300, 300);

        JPanel panel = new JPanel(new BorderLayout());

        // 创建根节点
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("中国");

        // 创建二级节点
        DefaultMutableTreeNode node01 = new DefaultMutableTreeNode("北京");
        DefaultMutableTreeNode node02 = new DefaultMutableTreeNode("上海");
        DefaultMutableTreeNode node03 = new DefaultMutableTreeNode("广东");
        DefaultMutableTreeNode node04 = new DefaultMutableTreeNode("浙江");

        // 把二级节点作为子节点添加到根节点
        rootNode.add(node01);
        rootNode.add(node02);
        rootNode.add(node03);
        rootNode.add(node04);

        // 创建三级节点
        DefaultMutableTreeNode child01 = new DefaultMutableTreeNode("北京市");

        DefaultMutableTreeNode child02 = new DefaultMutableTreeNode("上海市");

        DefaultMutableTreeNode child03 = new DefaultMutableTreeNode("广州市");
        DefaultMutableTreeNode child04 = new DefaultMutableTreeNode("深圳市");
        DefaultMutableTreeNode child05 = new DefaultMutableTreeNode("佛山市");

        DefaultMutableTreeNode child06 = new DefaultMutableTreeNode("杭州市");
        DefaultMutableTreeNode child07 = new DefaultMutableTreeNode("宁波市");

        // 把三级节点作为子节点添加到相应的二级节点
        node01.add(child01);

        node02.add(child02);

        node03.add(child03);
        node03.add(child04);
        node03.add(child05);

        node04.add(child06);
        node04.add(child07);

        // 使用根节点创建树组件
        JTree tree = new JTree(rootNode);

        // 设置树显示根节点句柄
        tree.setShowsRootHandles(true);

        // 设置树节点可编辑
        tree.setEditable(true);

        // 设置节点选中监听器
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                System.out.println("当前被选中的节点: " + e.getPath());
            }
        });

        // 创建滚动面板，包裹树（因为树节点展开后可能需要很大的空间来显示，所以需要用一个滚动面板来包裹）
        JScrollPane scrollPane = new JScrollPane(tree);

        // 添加滚动面板到那内容面板
        panel.add(scrollPane, BorderLayout.CENTER);

        // 设置窗口内容面板并显示
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
